<?php

class Question extends Controller {

    public function __construct() {
        parent::Controller();
        if (!$this->session->userdata('isAuthenticated')) {
            redirect(base_url().'/admin.php?c=auth&m=index');
        }
        $this->load->model('questionmodel', 'question');
    }

    public function index() {
        $this->load->library('pagination');

        $limit = 2;
		$offset= intval($this->input->get('per_page'));
        $total = $this->question->q_count();

        $config['base_url'] = base_url() . '/admin.php?c=question&m=index';
        $config['total_rows'] = $total;
        $config['per_page'] = $limit;
        $this->pagination->initialize($config);

        $data['title'] = '在线咨询';
        $data['questions'] = $this->question->q_list($limit, $offset);
        $data['page_links'] = $this->pagination->create_links();
        $this->load->view('question/index', $data);
    }

    public function create() {
        $this->load->helper('form');
        $data['title'] = '添加咨询问题';
        $this->load->view('question/create', $data);
    }

    public function add() {
        $this->load->library('form_validation');
		
		$this->form_validation->set_rules('author', '您的称呼', 'required');
		$this->form_validation->set_rules('title', '问题标题', 'required');
		$this->form_validation->set_rules('content', '问题内容', 'required');
		
		
        if ($this->form_validation->run() == false) {
            $data['title'] = '添加咨询问题';
            $this->load->view('question/create', $data);
        } else {
            $question = array(
                'author' => $this->input->post('author'),
                'ip' => $_SERVER['REMOTE_ADDR'],
                'title' => $this->input->post('title'),
                'content' => $this->input->post('content'),
                'created_at' => date('Y-m-d H:i:s', time())
            );

            if ($this->db->insert('question', $question)) {
                $this->session->set_flashdata('notice', '添加成功');
                redirect(base_url().'/admin.php?c=question&m=index');
            } else {
                $this->session->set_flashdata('error', '添加失败，请联系管理员！');
                redirect(base_url().'/admin.php?c=question&m=index');
            }
        }
    }

    public function edit() {
        $id = $this->getId();
        if ($question = $this->question->find($id)) {
            $this->load->helper('form');
            $data['question'] = $question;
            $this->load->view('question/edit', $data);
        } else {
            show_404();
        }
    }

    public function update() {
        $id = intval($this->input->post('id'));
        if ($this->question->find($id)) {
            $this->load->library('form_validation');
			
			$this->form_validation->set_rules('author', '您的称呼', 'required');
			$this->form_validation->set_rules('title', '问题标题', 'required');
			$this->form_validation->set_rules('content', '问题内容', 'required');
			
            if ($this->form_validation->run() == false) {
                $data['id'] = $id;
                $this->load->view('question/edit', $data);
            } else {
                $data['author'] = $this->input->post('author');
                $data['title'] = $this->input->post('title');
                $data['content'] = $this->input->post('content');
                if ($this->db->update('question', $data, array('id' => $id))) {
                    $this->session->set_flashdata('notice', '修改成功');
                    redirect(base_url().'/admin.php?c=question&m=index');
                } else {
                    $this->session->set_flashdata('error', '修改失败，请联系管理员！');
                    redirect(base_url().'/admin.php?c=question&m=index');
                }
            }
        } else {
            show_404();
        }
    }

    public function delete() {
        $id = $this->getId();
        if ($this->question->find($id)) {
            if ($this->db->delete('question', array('id' => $id))) {
                $this->session->set_flashdata('notice', '删除成功！');
                redirect(base_url().'/admin.php?c=question&m=index');
            }
        } else {
            show_404();
        }
    }

    public function reply() {
        $id = $this->getId();
        if ($q = $this->question->find($id)) {
            $this->load->helper('form');
            $data['q'] = $q;
            $data['title'] = '回复问题【'.$q->title.'】';
            $this->load->view('question/reply', $data);
        } else {
            show_404();
        }
    }

    public function addreply() {
        $id = intval($this->input->post('question_id'));
        if ($q = $this->question->find($id)) {
            $this->load->library('form_validation');
			$this->form_validation->set_rules('content', '回复内容', 'required');
			$this->form_validation->set_rules('name', '您的称呼', 'required');
            if ($this->form_validation->run() == false) {
                $data['q'] = $q;
                $data['title'] = '回复问题【'.$q->title.'】';
                $this->load->view('question/reply', $data);
            } else {
                $a['question_id'] = $id;
                $a['author'] = $this->input->post('author');
                $a['content'] = $this->input->post('content');
                $a['created_at'] = date('Y-m-d H:i:s', time());
                if ($this->db->insert('anwser', $a)) {
                    $this->session->set_flashdata('notice', '回复成功');
                    redirect(base_url().'admin.php?c=question&m=reply&id=' . $id);
                } else {
                    $this->session->set_flashdata('error', 'db error!');
                    redirect(base_url().'admin.php?c=question&m=reply&id=' . $id);
                }
            }
        } else {
            show_404();
        }
    }

    private function getId() {
        return intval($this->input->get('id'));
    }

}